package again.newlife.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class JsonTest02 {
    public static void main(String[] args) {
        //创建Person对象数组
        Person[] people = {new Person("张三", 18), new Person("王五", 19), new Person("赵六", 20)};
        //将Person对象数组转换成JSON字符串
        String s = JSON.toJSONString(people);
        //将JSON字符串转化为JSON数组对象
        JSONArray jsonArray = JSON.parseArray(s);
        for (Object o : jsonArray) {
            //将JSON元素强转为JOSONObject对象
            JSONObject jsonObject = (JSONObject) o;
            System.out.println(jsonObject.get("name") + ":" + jsonObject.get("age"));
        }
    }

    public static void main1(String[] args) throws  Exception{
        //创建Person对象数组
        BufferedReader br = new BufferedReader(new FileReader("day14-code/src/again/newlife/file/person.json"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("day14-code/src/again/newlife/file/person2.json"));
        String s = null;
        while ((s = br.readLine()) != null){
            bw.append(s.trim());
            bw.newLine();
            bw.flush();
        }
        //关闭流
        br.close();
        bw.close();
        System.out.println("复制完毕");
    }

}
